package com.wetsion.securityoauthdemo.rmsclient.web;

import com.alibaba.fastjson.JSONObject;
import com.wetsion.securityoauthdemo.rmsclient.common.RmsClientDetailsProperties;
import com.wetsion.securityoauthdemo.rmsclient.common.RmsOauthClientContext;
import com.wetsion.securityoauthdemo.rmsclient.common.RmsOauthConstant;
import com.wetsion.securityoauthdemo.rmsclient.feign.support.AuthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
import org.springframework.security.oauth2.client.OAuth2RestOperations;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author weixin
 * @version 1.0
 * @CLassName TestController
 * @date 2019/3/7 5:39 PM
 */
@RestController
public class TestController {

    private final static Logger logger = LoggerFactory.getLogger(TestController.class);
//
//    @Autowired
//    OAuth2RestOperations restOperations;

    @GetMapping("/test1")
    public void test1() {
//        OAuth2AccessToken accessToken = restOperations.getAccessToken();
//        System.out.println(accessToken.getValue());
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor("rms", "weixin"));
//        JSONObject params = new JSONObject();
        MultiValueMap<String, String> params= new LinkedMultiValueMap<>();
//        params.put("grant_type", "password");
//        params.put("username", "weixin");
//        params.put("password", "weixin");
//        params.put("client_id", "rms");
//        params.put("client_secret", "weixin");
        params.add("grant_type", "password");
        params.add("username", "weixin");
        params.add("password", "weixin");
        params.add("client_id", "rms");
        params.add("client_secret", "weixin");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://localhost:9001/oauth/token", entity, String.class);
        System.out.println(responseEntity.getBody());
        System.out.println("test1");
    }

    @GetMapping("/api/test2")
    public void test2(HttpServletRequest request) {
        System.out.println(request.getAttribute("currentUri"));
//        OAuth2AccessToken accessToken = restOperations.getAccessToken();
        System.out.println("test2");
    }

    @GetMapping("/home")
    public String home() {
        System.out.println("home");
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getInterceptors().add(
                new BasicAuthenticationInterceptor(rmsClientDetailsProperties.getClientId(), rmsClientDetailsProperties.getClientSecret()));
        MultiValueMap<String, String> params= new LinkedMultiValueMap<>();
        params.add(RmsOauthConstant.TOKEN, "b90c504d-a6a7-41e0-907f-219e928b2626");
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, headers);
        ResponseEntity<String> responseEntity = null;
        try {
            responseEntity = restTemplate.postForEntity(
                    rmsClientDetailsProperties.getCheckTokenUrl(), entity, String.class);
            System.out.println(responseEntity.getBody());
        } catch (Exception e) {
            logger.info("从远程auth校验认证: {} 失败: {}", "b90c504d-a6a7-41e0-907f-219e928b2626", e.getMessage());
        }
        return "home";
    }

    @Autowired
    RmsClientDetailsProperties rmsClientDetailsProperties;

    @Autowired
    RmsOauthClientContext rmsOauthClientContext;

//    @Autowired
//    TestFeign testFeign;

    @Autowired
    AuthService authService;

    @Autowired
    ApplicationContext applicationContext;

    @GetMapping("/login")
    public String login() {
        System.out.println("login");
//        System.out.println(rmsOauthClientContext.getAccessToken().getAccessToken());
//        TestFeign testFeign = applicationContext.getBean(TestFeign.class);
//        testFeign.getResources("wxname");
        JSONObject result = authService.resources("weixin", "rms");
        System.out.println(result.toJSONString());
//        String r2 = authService.test("{\"name\": \"weixin\"}");
//        System.out.println(r2);
//        String r3 = authService.test2("weixin");
//        System.out.println(r3);
        System.out.println(Arrays.toString(applicationContext.getBeanDefinitionNames()));
        return "login";
    }

    @GetMapping("/")
    public void index() {
        System.out.println("index");
    }
}
